package com.zebra.remoting.tms;

import com.zebra.common.ZebraBaseMessage;
import com.zebra.remoting.config.*;
import com.zebra.remoting.connection.Connection;
import com.zebra.remoting.protocol.ZebraCommand;
import io.netty.channel.Channel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

public class TmsSendMessageUtil {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(TmsSendMessageUtil.class);

    public static void pushMessage(Channel channel, int cmd, long userId, long tk, ZebraBaseMessage message){
       ZebraCommand command = ZebraCommand.builder()
               .channel(channel)
               .cmd(cmd)
               .token(tk)
               .userId(userId)
               .reserve(BasisCommandCode.PUSHCMD.cmd())
               .build();
       command.sendMessage(message);
    }

    //true 表示不打印response 日志
    public static void pushMessage(Channel channel, int cmd, long userId, long tk, ZebraBaseMessage message, boolean isLog){
       ZebraCommand command = ZebraCommand.builder()
               .channel(channel)
               .cmd(cmd)
               .token(tk)
               .userId(userId)
               .reserve(BasisCommandCode.PUSHCMD.cmd())
               .logs(isLog)
               .build();
       command.sendMessage(message);
    }

    public static void sendStateCode(Channel channel, int cmd, long userId, long tk, int stateCode){
        ZebraCommand command = ZebraCommand.builder()
                .channel(channel)
                .cmd(cmd)
                .token(tk)
                .userId(userId)
                .build();
        command.sendMessage(StateMessage.builder().state(stateCode).build());
    }

    public static void sendLinksToRA(){
        String linkTms = TmsConfigInfoManager.getInstance().buildLinkTms();
        Connection registerConn = TmsConfigInfoManager.getInstance().getRAConnection();
        if (null == registerConn){
            logger.error("Send links gateway register is null; gw {}", TmsConfigInfoManager.getInstance().getServerInfo().toString()); //如果没有注册中心
            return;
        }
        LINKSTORAmessage message = new LINKSTORAmessage();
        message.setServerId(TmsConfigInfoManager.getInstance().getServerInfo().getServerId());
        message.setServerType(ServerType.Tms.value());
        message.setLinks(linkTms);

        ZebraCommand command = ZebraCommand.builder()
                .channel(registerConn.getChannel())
                .cmd(BasisCommandCode.LINKSTORA.cmd())
                .build();
        command.sendMessage(message);
    }

    public static void sendOnlineNumToRA(long onlineNumber){
        Connection registerConn = TmsConfigInfoManager.getInstance().getRAConnection();
        if (null == registerConn){
            logger.error("Send onlineNumber to register is null; gw {}", TmsConfigInfoManager.getInstance().getServerInfo().toString());//如果没有注册中心
            return;
        }

        ONNUMTORAmessage message = new ONNUMTORAmessage();
        message.setServerId(TmsConfigInfoManager.getInstance().getServerInfo().getServerId());
        message.setServerType(ServerType.Tms.value());
        message.setOnlineNumber(onlineNumber);
        if (logger.isInfoEnabled()){
            logger.info("Send Send onlineNumber to register {}", message);
        }

        ZebraCommand command = ZebraCommand.builder()
                .channel(registerConn.getChannel())
                .cmd(BasisCommandCode.ONNUMTORA.cmd())
                .build();
        command.sendMessage(message);
    }


}
